package com.example.web.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.utils.MakeMenuTree;
import com.example.web.system.entity.*;
import com.example.web.system.mapper.SysMenuMapper;
import com.example.web.system.mapper.SysRoleMenuMapper;
import com.example.web.system.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kimido
 * @since 2024-07-04
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public void saveMenuIdsByRoleId(SaveMenuIdsByRoleIdParam param) {
        Integer roleId = param.getRoleId();
        //清空跟用户有关联的菜单关系
        LambdaQueryWrapper<SysRoleMenu> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId,roleId);
        sysRoleMenuMapper.delete(queryWrapper);


        List<Integer> menuIds = param.getMenuIds();
        if(!Objects.isNull(menuIds)&& menuIds.size()>0){
            for (Integer menuId:menuIds) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenu.setDeleted(1);
                sysRoleMenuMapper.insert(sysRoleMenu);
             }
        }
    }

    @Override
    public CheckMenuTreeVo getAssignTree(Integer roleId) {
        //查询树形菜单数据
        List<SysMenu> list = this.getList();
        //查询角色原理拥有的菜单id
        LambdaQueryWrapper<SysRoleMenu> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId,roleId);
        List<SysRoleMenu> roleMenus = sysRoleMenuMapper.selectList(queryWrapper);
        List<Integer> roleMenusIds=roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());

        //封装数据
        CheckMenuTreeVo checkMenuTreeVo =new CheckMenuTreeVo();
        checkMenuTreeVo.setTree(list);
        checkMenuTreeVo.setCheckMenuIds(roleMenusIds);
        return checkMenuTreeVo;
    }



    @Override
    public List<SysMenu> getList() {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> menus =this.baseMapper.selectList(queryWrapper);

        //封装成树形数据
        List<SysMenu> res = MakeMenuTree.makeTree(menus, 0);
        return res;
    }

    @Override
    public List<SysMenu> getParentList() {
        //只 查询目录和菜单即只查询0和1的数据
        Integer[] types={0,1};

        //构造查询条件
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysMenu::getType,types).orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> menus = this.baseMapper.selectList(queryWrapper);

        //构造 顶级 菜单
        SysMenu topMenu = new SysMenu();
        topMenu.setMenuId(0);
        topMenu.setParentId(-1);
        topMenu.setModuleLabel("顶级菜单");

        menus.add(topMenu);

        //构造树形菜单
        List<SysMenu> res = MakeMenuTree.makeTree(menus, -1);
        return  res;
    }
}
